package logic

import (
	"context"
	"fmt"
	"housing/models/mysql"

	"housing/houserpc/houserpc"
	"housing/houserpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdatePasswordLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdatePasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdatePasswordLogic {
	return &UpdatePasswordLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdatePasswordLogic) UpdatePassword(in *houserpc.UpdatePasswordRequest) (*houserpc.UpdatePasswordResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User
	mysql.DB.Debug().Where("mobile=?", in.Mobile).Find(&user)
	//sms, _ := cache.Rdb.Get(l.ctx, "sendSms"+in.SmsCode).Result()
	//if sms != in.SmsCode {
	//	return nil, fmt.Errorf("短信验证码错误,请重新输入")
	//}
	if user.ID == 0 {
		return nil, fmt.Errorf("手机号不存在,不允许修改")
	}

	if in.Password != in.NewPassword {
		return nil, fmt.Errorf("新密码与旧密码不一致,请重新输入")
	}
	if in.Password == user.Password {
		return nil, fmt.Errorf("新密码与旧密码一致,请重新输入")
	}
	user.Password = in.Password
	mysql.DB.Debug().Updates(&user)
	return &houserpc.UpdatePasswordResponse{
		UserId: int64(user.ID),
	}, nil
}
